﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace LPV_POS.In
{
    class print_BIEN_NHAN_MOI
    {

        public Boolean Print_PHIEU_BIENNHAN(string _maPhieu)
        {
            #region "   LoadData BH_PHIEU-BH_PHIEU_CT"

            new Variable().Get_DM_MAYIN_BIENNHAN();
            //Mo cong com
            DataRow _drPhieu = null;
            DataTable _tbPhieuCT = null;
            DataTable _tbPhieu = this.getTable_Phieu("", _maPhieu + "");
            string maPhieu = "";

            if (_tbPhieu.Rows.Count == 1)
                maPhieu = _tbPhieu.Rows[0]["OBJID"] + "";
            else
                return false;

            if (_tbPhieu.Select("OBJID LIKE '" + maPhieu + "'").Length > 0)
                _drPhieu = _tbPhieu.Select("OBJID LIKE '" + maPhieu + "'")[0];

            string sSQL = "";
            sSQL =
                "SELECT MIN(aa.OBJID), aa.OBJNAME, aa.BH_PHIEU_MA, aa.MAHH, SO_LUONG = SUM(aa.SO_LUONG), aa.DVT, DON_GIA = MAX(aa.DON_GIA), " +
                "       CHIETKHAU_PHANTRAM = MAX(aa.CHIETKHAU_PHANTRAM), CHIETKHAU_TIEN = MAX(aa.CHIETKHAU_TIEN), THANH_TIEN = SUM(aa.THANH_TIEN),  " +
                "       aa.VAT_PHANTRAM, aa.VAT_TIEN " +
                "FROM  " +
                "(  " +
                "    SELECT b.OBJID, OBJNAME = (CASE b.MAHH WHEN 'Tien_Gio' THEN N'Tiền giờ' ELSE h.OBJNAME END ), b.BH_PHIEU_MA, " +
                "           b.MAHH, b.SO_LUONG, b.DVT, b.DON_GIA, b.CHIETKHAU_PHANTRAM, " +
                "           b.CHIETKHAU_TIEN, b.THANH_TIEN, b.VAT_PHANTRAM, b.VAT_TIEN " +
                "    FROM BH_PHIEU_CT b " +
                "        LEFT JOIN DM_HH h ON b.MAHH = h.OBJID  " +
                "    WHERE b.BH_PHIEU_MA LIKE '" + maPhieu + "'  " +
                ")aa " +
                "GROUP BY aa.OBJNAME, aa.BH_PHIEU_MA, aa.MAHH, aa.DVT, aa.VAT_PHANTRAM, aa.VAT_TIEN   " +
                "HAVING SUM(aa.SO_LUONG) > 0 " +
                "ORDER BY MIN(aa.OBJID)";

            _tbPhieuCT = new thuvien().SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });

            #endregion

            return true;

        }



        #region "   getTable_Phieu"
        private DataTable getTable_Phieu(string maBan, string maPhieu)
        {
            string sSQL = "";
            if (maBan != "")
                maBan = " AND P.DM_BAN_MA LIKE '" + maBan + "' ";
            if (maPhieu != "")
                maPhieu = " AND p.OBJID LIKE '" + maPhieu + "'";

            sSQL = "SELECT b.OBJNAME, p.OBJID, p.DM_BAN_MA, NGUOI_LAP = ISNULL(n.OBJNAME, p.NGUOI_LAP), p.NGAY_GIO_VAO, p.NGAY_GIO_RA, " +
                   "        p.TINH_TIEN_THEO_NGAY, p.TIEN_GIO, p.THANH_TIEN, p.CK_PHANTRAM, p.CK_TIEN, " +
                   "        CK_THUCAN_PHANTRAM = ISNULL(p.CK_THUCAN_PHANTRAM, 0), CK_THUCAN_TIEN = ISNULL(p.CK_THUCAN_TIEN, 0), " +//20111110
                   "        CK_THUCUONG_PHANTRAM = ISNULL(p.CK_THUCUONG_PHANTRAM, 0), CK_THUCUONG_TIEN = ISNULL(p.CK_THUCUONG_TIEN, 0), " +//20111110

                   "        p.VAT_PHANTRAM, p.VAT_TIEN, p.TTIEP_PHANTRAM, p.TTIEP_TIEN, " +
                   "        p.PHU_THU_PHANTRAM, p.PHU_THU, p.PHU_THU_KHAC, p.TONG_TIEN, p.GHI_CHU, " +
                   "        NGAYHH = CONVERT(VARCHAR(30), p.NGAY_GIO_RA, 103) +' '+ CONVERT(VARCHAR(30), p.NGAY_GIO_RA, 108), " +
                   "        p.TIEN_DA_NHAN, " +
                   "        DAT_COC = (SELECT DAT_COC FROM DM_BAN_DAT_BAN WHERE BH_PHIEU_ID = p.OBJID ) " +
                   "FROM BH_PHIEU p " +
                   "        LEFT JOIN DM_BAN b ON p.DM_BAN_MA = b.OBJID " +
                   "        LEFT JOIN DM_DANGNHAP d ON p.NGUOI_LAP = d.USERNAME " +
                   "        LEFT JOIN DM_NHANVIEN n ON d.MA_NHANVIEN = n.OBJID " +
                   "WHERE 1 = 1 " + maBan + maPhieu
                ;
            DataTable _tbPhieu = new thuvien().SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            return _tbPhieu;
        }
        #endregion

    }
}
